package com.atguigu.srb.core.controller.api;


import com.atguigu.common.exception.Assert;
import com.atguigu.common.result.R;
import com.atguigu.common.result.ResponseEnum;
import com.atguigu.common.util.RegexValidateUtils;
import com.atguigu.srb.base.util.JwtUtils;
import com.atguigu.srb.core.pojo.vo.LoginVO;
import com.atguigu.srb.core.pojo.vo.RegisterVO;
import com.atguigu.srb.core.pojo.vo.UserIndexVO;
import com.atguigu.srb.core.pojo.vo.UserInfoVO;
import com.atguigu.srb.core.service.UserInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 用户基本信息 前端控制器
 * </p>
 *
 * @author zwj
 * @since 2021-10-08
 */
@RestController
@RequestMapping("/api/core/userInfo")
@Slf4j
//@CrossOrigin
@Api(tags = "用户管理")
public class UserInfoController {
    @Autowired
    private UserInfoService userInfoService;

    @ApiOperation("用户注册")
    @PostMapping("/register")
    public R register(
            @ApiParam(value = "注册信息",required = true)
            @RequestBody RegisterVO registerVO
            ){
        String mobile = registerVO.getMobile();
        String password = registerVO.getPassword();
        String code = registerVO.getCode();

        //MOBILE_NULL_ERROR(-202, "手机号不能为空"),
        Assert.notEmpty(mobile, ResponseEnum.MOBILE_NULL_ERROR);
        //MOBILE_ERROR(-203, "手机号不正确"),
        Assert.isTrue(RegexValidateUtils.checkCellphone(mobile), ResponseEnum.MOBILE_ERROR);
        //PASSWORD_NULL_ERROR(-204, "密码不能为空"),
        Assert.notEmpty(password, ResponseEnum.PASSWORD_NULL_ERROR);
        //CODE_NULL_ERROR(-205, "验证码不能为空"),
        Assert.notEmpty(code, ResponseEnum.CODE_NULL_ERROR);
        userInfoService.register(registerVO);
        return R.ok().message("注册成功");
    }

    @ApiOperation("用户登录")
    @PostMapping("/login")
    public R login(
            @ApiParam(value = "登录信息",required = true)
            @RequestBody LoginVO loginVO,
            HttpServletRequest request
            ){
        Assert.notNull(loginVO.getMobile(),ResponseEnum.MOBILE_NULL_ERROR);
        Assert.notNull(loginVO.getPassword(),ResponseEnum.PASSWORD_NULL_ERROR);
        String ip = request.getRemoteAddr();

        UserInfoVO userInfoVO=userInfoService.login(loginVO,ip);
        return R.ok().message("登陆成功").data("userInfo",userInfoVO);
    }

    @ApiOperation("token校验")
    @GetMapping("/checkToken")
    public R checkToken(@ApiParam(value = "http请求",required = true) HttpServletRequest request){
        String token = request.getHeader("token");
        if (JwtUtils.checkToken(token)) {
            return R.ok();
        } else {
            //LOGIN_AUTH_ERROR(-211, "未登录"),
            return R.setResult(ResponseEnum.LOGIN_AUTH_ERROR);
        }
    }

    @ApiOperation("检验手机号是否已经注册")
    @GetMapping("/checkMobile/{mobile}")
    public boolean checkMobile(
            @ApiParam(value = "手机号",required = true)
            @PathVariable("mobile") String mobile){
        return userInfoService.checkMobile(mobile);
    }


    @ApiOperation("获取个人空间用户信息")
    @GetMapping("/auth/getIndexUserInfo")
    public R getIndexUserInfo(HttpServletRequest request) {
        String token = request.getHeader("token");
        Long userId = JwtUtils.getUserId(token);
        UserIndexVO userIndexVO = userInfoService.getIndexUserInfo(userId);
        return R.ok().data("userIndexVO", userIndexVO);
    }
}

